package com.borya.action.mobile;

import com.alibaba.fastjson.JSONObject;
import com.borya.action.BaseAction;
import com.borya.framework.annotation.Action;
import com.borya.framework.annotation.Autowired;
import com.borya.framework.annotation.JsonResult;
import com.borya.framework.annotation.UrlMapping;
import com.borya.framework.core.ServerRequest;
import com.borya.framework.core.ServerResponse;
import com.borya.model.db.User;
import com.borya.model.entity.HeaderEntity;
import com.borya.model.entity.UserToken;
import com.borya.service.mobile.SdkUserService;
import org.apache.log4j.Logger;

/**
 * 描述:
 * SDK相关
 *
 * @Author: CYH
 * @Date: 2020/3/18 11:35
 */
@UrlMapping("/c/sdkUser/")
@Action
public class SdkUserAction extends BaseAction {

    private Logger log = Logger.getLogger(getClass());

    @Autowired("sdkUserServiceImpl")
    private SdkUserService sdkUserService;

    /**
     * SDK商户激活提交接口
     * @param request
     * @param response
     * @return
     */
    @UrlMapping("saveMerchantActivation")
    @JsonResult
    public String saveMerchantActivation(ServerRequest request, ServerResponse response) {
        UserToken userToken = getUserToken(request);
        JSONObject json = getJson(request);
        HeaderEntity authHeader = getAuthHeader(request);
        log.info("INFO[" + json + "]");
        String result = sdkUserService.saveMerchantActivation(json, userToken, authHeader);
        log.info("INFO [" + json + "],OUT[" + result + "]");
        return result;
    }

    /**
     * SDK商户激活/补录预审接口
     * @param request
     * @param response
     * @return
     */
    @UrlMapping("merchantActivationPreAudit")
    @JsonResult
    public String merchantActivationPreAudit(ServerRequest request, ServerResponse response) {
        JSONObject json = getJson(request);
        User user = getUser(request);
        log.info("INFO [" + json + "]");
        String result = sdkUserService.merchantActivationPreAudit(user, json);
        log.info("INFO[" + json + "],OUT[" + result + "]");
        return result;
    }

    /**
     * 回调SDK商户激活/补录预审结果接口
     * @param request
     * @param response
     * @return
     */
    @UrlMapping("preAuditBack")
    @JsonResult
    public String preAuditBack(ServerRequest request, ServerResponse response) {
        JSONObject json = getJson(request);
        User user = getUser(request);
        log.info("INFO[" + json + "]");
        String result = sdkUserService.preAuditBack(user, json);
        log.info("INFO[" + json + "],OUT[" + result + "]");
        return result;
    }

    /**
     * SDK商户补录提交接口
     */
    @UrlMapping("saveMerchantFill")
    @JsonResult
    public String saveMerchantFill(ServerRequest request, ServerResponse response){
        JSONObject json = getJson(request);
        User user = getUser(request);
        log.info("INFO [" + json + "]");
        String result = sdkUserService.saveMerchantFill(user, json);
        log.info("INFO [" + json + "],OUT[" + result + "]");
        return result;
    }

    /**
     * SDK商户激活校验企业营业执照数量的接口
     */
    @UrlMapping("checkNumByBusinessLicence")
    @JsonResult
    public String checkNumByBusinessLicence(ServerRequest request, ServerResponse response){
        JSONObject json = getJson(request);
        User user = getUser(request);
        log.info("INFO [" + json + "]");
        String result = sdkUserService.checkNumByBusinessLicence(user, json);
        log.info("INFO [" + json + "], OUT[" + result + "]");
        return result;
    }

    /**
     * 获取激活订单状态
     */
    @UrlMapping("getActivationOrderStatus")
    @JsonResult
    public String getActivationOrderStatus(ServerRequest request, ServerResponse response) {
        UserToken userToken = getUserToken(request);
        HeaderEntity authHeader = getAuthHeader(request);
        log.info("INFO[userToken:" + JSONObject.toJSONString(userToken) + " ,authHeader: " + JSONObject.toJSONString(authHeader) + "]");
        String result = sdkUserService.getActivationOrderStatus(userToken, authHeader);
        log.info("OUT[" + result + "]");
        return result;
    }
}
